﻿using Elasticsearch.Net;
using Nest;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Developer.Core.FrameWork.ElasticsearchHelp
{
    public interface IElasticsearchServer
    {
        /// <summary>
        /// linq查询client
        /// </summary>
        /// <returns></returns>
        IElasticClient ElasticLinqClient { get; set; }

        /// <summary>
        /// json查询client
        /// </summary>
        IElasticLowLevelClient ElasticJsonClient { get; set; }

        /// <summary>
        /// linq查询方式
        /// </summary>
        /// <typeparam name="T">要查询返回的对象</typeparam>
        /// <param name="indexName">index名字</param>
        /// <param name="typeName">type名字</param>
        /// <param name="selector">linq内容</param>
        /// <returns></returns>
        Task<List<T>> SearchAsync<T>(string indexName, string typeName, Func<QueryContainerDescriptor<T>, QueryContainer> selector = null)
            where T : class;

        /// <summary>
        /// json查询方式
        /// </summary>
        /// <param name="indexName">index名字</param>
        /// <param name="typeName">type名字</param>
        /// <param name="jsonString">json字符串</param>
        /// <returns></returns>
        Task<JToken> SearchAsync(string indexName, string typeName, string jsonString);

        /// <summary>
        /// 创建index
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <param name="shards">分片数量</param>
        /// <returns></returns>
        Task<bool> CreateIndexAsync(string indexName, int shards = 5);

        /// <summary>
        /// 删除index
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <returns></returns>
        Task<bool> DeleteIndexAsync(string indexName);

        /// <summary>
        /// 新增文档
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <param name="typeName">type名称</param>
        /// <param name="objectDocument">文档内容</param>
        /// <param name="_id">自定义id</param>
        /// <returns></returns>
        Task<bool> InsertDocumentAsync(string indexName, string typeName, object objectDocument, string _id = "");

        /// <summary>
        /// 批量新增文档
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <param name="typeName">type名称</param>
        /// <param name="listDocument">数据集合</param>
        /// <returns></returns>
        Task<bool> InsertListDocumentAsync(string indexName, string typeName, List<object> listDocument);

        /// <summary>
        /// 删除文档
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <param name="typeName">type名称</param>
        /// <param name="_id">elasticsearch的id</param>
        /// <returns></returns>
        Task<bool> DeleteDocumentAsync(string indexName, string typeName, string _id);

        /// <summary>
        /// 更新文档
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <param name="typeName">type名称</param>
        /// <param name="_id">elasticsearch的id</param>
        /// <param name="objectDocument">单条数据的所有内容</param>
        /// <returns></returns>
        Task<bool> UpdateDocumentAsync(string indexName, string typeName, string _id, object objectDocument);

        /// <summary>
        /// 批量更新文档
        /// </summary>
        /// <param name="indexName">index名称</param>
        /// <param name="typeName">type名称</param>
        /// <param name="listDocument">数据集合</param>
        /// <returns></returns>
        Task<bool> UpdateListDocumentAsync(string indexName, string typeName, List<object> listDocument);
    }
}
